package com.changhong.yutong.admin.modules.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * Created by Administrator on 2018/3/26
 */
@Slf4j
public class ShiroRealm extends AuthorizingRealm {

    /**
     * Retrieves the AuthorizationInfo for the given principals from the underlying data store.  When returning
     * an instance from this method, you might want to consider using an instance of
     * {@link SimpleAuthorizationInfo SimpleAuthorizationInfo}, as it is suitable in most cases.
     *
     * @param principals the primary identifying principals of the AuthorizationInfo that should be retrieved.
     * @return the AuthorizationInfo associated with this principals.
     * @see SimpleAuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    /**
     * Retrieves authentication data from an implementation-specific datasource (RDBMS, LDAP, etc) for the given
     * authentication token.
     * <p/>
     * For most datasources, this means just 'pulling' authentication data for an associated subject/user and nothing
     * more and letting Shiro do the rest.  But in some systems, this method could actually perform EIS specific
     * log-in logic in addition to just retrieving data - it is up to the Realm implementation.
     * <p/>
     * A {@code null} return value means that no account could be associated with the specified token.
     *
     * @param token the authentication token containing the user's principal and credentials.
     * @return an {@link AuthenticationInfo} object containing account data resulting from the
     * authentication ONLY if the lookup is successful (i.e. account exists and is valid, etc.)
     * @throws AuthenticationException if there is an error acquiring data or performing
     *                                 realm-specific authentication logic for the specified <tt>token</tt>
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        String password = "";
        if (upToken.getPassword() != null)
        {
            password = new String(upToken.getPassword());
        }

//        User user = null;
//        try
//        {
//            user = loginService.login(username, password);
//        }
//        catch (CaptchaException e)
//        {
//            throw new AuthenticationException(e.getMessage(), e);
//        }
//        catch (UserNotExistsException e)
//        {
//            throw new UnknownAccountException(e.getMessage(), e);
//        }
//        catch (UserPasswordNotMatchException e)
//        {
//            throw new IncorrectCredentialsException(e.getMessage(), e);
//        }
//        catch (UserPasswordRetryLimitExceedException e)
//        {
//            throw new ExcessiveAttemptsException(e.getMessage(), e);
//        }
//        catch (UserBlockedException e)
//        {
//            throw new LockedAccountException(e.getMessage(), e);
//        }
//        catch (RoleBlockedException e)
//        {
//            throw new LockedAccountException(e.getMessage(), e);
//        }
//        catch (Exception e)
//        {
//            log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
//            throw new AuthenticationException(e.getMessage(), e);
//        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(new Object(), password, getName());
        return info;
    }

//    @Override
//    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
//        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//        hashedCredentialsMatcher.setHashAlgorithmName(ShiroUtils.ALGORITHMNAME);
//        hashedCredentialsMatcher.setHashIterations(ShiroUtils.HASHITERATIONS);
//        super.setCredentialsMatcher(hashedCredentialsMatcher);
//    }

}
